package com.content.controller
{
	import com.content.AppFacade;
	import com.content.model.Communication.WebAPIProxy;
	import com.content.model.GlobalVarProxy;
	import com.content.model.LoadedAssets.ConfigProxy;
	import com.content.model.LoadedAssets.FontProxy;
	import com.content.model.LoadedAssets.PatternFeedProxy;
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.AppMediator;
	import com.content.view.Frame.CBar.CBar;
	import com.content.view.Frame.CBar.CBarMediator;
	import com.content.view.Frame.Content.Content;
	import com.content.view.Frame.Content.ContentMediator;
	import com.content.view.Frame.InfoTape.InfoTape;
	import com.content.view.Frame.InfoTape.InfoTape_Mediator;
	import com.content.view.Share.Progress.Progress;
	import com.content.view.Share.Progress.ProgressMediator;
	
	import flash.system.fscommand;
	
	import org.puremvc.as3.interfaces.ICommand;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.command.SimpleCommand;
	import org.puremvc.as3.utilities.startupmanager.controller.StartupResourceFailedCommand;
	import org.puremvc.as3.utilities.startupmanager.controller.StartupResourceLoadedCommand;
	import org.puremvc.as3.utilities.startupmanager.interfaces.IStartupProxy;
	import org.puremvc.as3.utilities.startupmanager.model.StartupMonitorProxy;
	import org.puremvc.as3.utilities.startupmanager.model.StartupResourceProxy;

	public class AppCommand extends SimpleCommand implements ICommand
	{
		/**
		 * The "StartupMonitorProxy" takes care of how the resource loading should be sequenced 
		 * so that dependent resources are loaded in correct order. 
		 * It also keeps track of the progress of the resource loading, 
		 * and it knows when the resource loading is complete.
		 */
		private var _monitor:StartupMonitorProxy;
		
		/**
		 * For each startup resource the application must instantiate a "StartupResourceProxy" object. 
		 * These objects are required only by the utility and must not be confused with the 
		 * normal resource proxies that represent the data carrier objects of the model.
		 * 
		 * The regular proxies are instantiated and registered with the application first. 
		 * After that the StartupResourceProxy objects are instantiated, 
		 * receiving the resource proxy instance and a separate startup resource name (SRName).
		 * The two proxy types are like twins, but they live in different worlds.
		 */
		
		override public function execute(note:INotification):void
		{
			var name:String = note.getName();
			var body:Object = note.getBody();
			
			switch ( name )
			{
				/* "Startup" Execute before the res loaded in App
				* -------------------------------------------------------- */
				case AppFacade.STARTUP:
					/**
					 * In order to inform the monitor that a resource has been loaded successfully or that the resource load has failed, 
					 * the application must register the utility commands StartupResourceLoadedCommand and StartupResourceFailedCommand. 
					 * These commands are responsible for sending “Loaded” and “Failed” notifications 
					 * to the monitor whereby the notification body identifies the particular resource.
					 */
					facade.registerCommand( FontProxy.LOAD_COMPLETE, StartupResourceLoadedCommand );
					facade.registerCommand( FontProxy.LOAD_ERROR, StartupResourceFailedCommand );			
					facade.registerCommand( SkinProxy.LOAD_COMPLETE, StartupResourceLoadedCommand );
					facade.registerCommand( SkinProxy.LOAD_ERROR, StartupResourceFailedCommand );			
					facade.registerCommand( ConfigProxy.LOAD_COMPLETE, StartupResourceLoadedCommand );
					facade.registerCommand( ConfigProxy.LOAD_ERROR, StartupResourceFailedCommand );
					facade.registerCommand( PatternFeedProxy.LOAD_COMPLETE, StartupResourceLoadedCommand );
					facade.registerCommand( PatternFeedProxy.LOAD_ERROR, StartupResourceFailedCommand );
					
					/**
					 * Content Command 命令集合
					 */
					facade.registerCommand( AppFacade.MAINPANEL_OPEN, ContentCommand );
					
					
					facade.registerProxy( new GlobalVarProxy());
					
					facade.registerMediator( new AppMediator( body as App ) );
					sendNotification( App.RESIZE_MANUAL);	// Reposition the progress bar
					
					/* =Begin= StartupManager
					* -------------------------------------------------------- */
					sendNotification( Progress.SHOW );
					
					var skinProxy:IStartupProxy = new SkinProxy();
					var configProxy:IStartupProxy = new ConfigProxy();			
					var patternFeedProxy:IStartupProxy = new PatternFeedProxy();			
					var fontProxy:IStartupProxy = new FontProxy();	
					
					facade.registerProxy( skinProxy );
					facade.registerProxy( configProxy );
					facade.registerProxy( patternFeedProxy );
					facade.registerProxy( fontProxy );
					
					facade.registerProxy( new StartupMonitorProxy() );
					
					_monitor = facade.retrieveProxy( StartupMonitorProxy.NAME ) as StartupMonitorProxy;
					_monitor.defaultTimeout = 30;
					
					var rSkinProxy:StartupResourceProxy = makeAndRegisterStartupResource( SkinProxy.SRNAME, skinProxy );
					var rConfigProxy:StartupResourceProxy = makeAndRegisterStartupResource( ConfigProxy.SRNAME, configProxy );
					var rPatternFeedProxy:StartupResourceProxy = makeAndRegisterStartupResource( PatternFeedProxy.SRNAME, patternFeedProxy );
					var rFontProxy:StartupResourceProxy = makeAndRegisterStartupResource( FontProxy.SRNAME, fontProxy );
					
					// The dependency between the two resources is specified.
					// The site data requires that the stylesheet must be loaded first.
					//rConfigProxy.requires = [ rSkinProxy ];			
//					rFontProxy.requires = [ rConfigProxy ];			
					
					_monitor.loadResources();
					/* =End= StartupManager
					* -------------------------------------------------------- */
					break;
				
				/* "App_Init" Execute after the res loaded in App
				* -------------------------------------------------------- */
				case AppFacade.APP_INIT:
					
					// 注册 WebAPI 代理
					facade.registerProxy( new WebAPIProxy(pConfig.Feed_URL, pConfig.UserID));
					
					// 根据  xml 文件的格式布置  CBar 菜单
					sendNotification( CBar.INIT);
					sendNotification( App.RESIZE_MANUAL);
					
					sendNotification( Progress.HIDE);
					sendNotification( Content.SHOW, {delay: 0, duration: 0.5} );
					sendNotification( CBar.SHOW, {delay: 0.5, duration: 0.5} );
					sendNotification( InfoTape.SHOW, {delay: 0.5, duration: 0.5} );
					
					pWebAPI.GetPersonalInfo(
						function(xml:XML):void{
							mInfoTape.InfoUpdate(xml);
						}
					);
					
					if(pConfig.Debug){
						// 调试代码 
//						sendNotification(AppFacade.PANEL_OPEN, {MPID: "MPOnlineResBook"});
					}else{
						// 正式代码
					}
					break;
				
				case AppFacade.APP_QUIT:
					fscommand("quit");
					break;
			}
		}
		
		private function makeAndRegisterStartupResource( proxyName:String, appResourceProxy:IStartupProxy ):StartupResourceProxy
		{
			var r:StartupResourceProxy = new StartupResourceProxy( proxyName, appResourceProxy );
			facade.registerProxy( r );
			_monitor.addResource( r );
			return r;
		}
		
		private function get mProgress():ProgressMediator{return facade.retrieveMediator( ProgressMediator.NAME ) as ProgressMediator;}
		private function get mInfoTape():InfoTape_Mediator {return facade.retrieveMediator(InfoTape_Mediator.NAME) as InfoTape_Mediator};
		private function get mContent():ContentMediator{return facade.retrieveMediator( ContentMediator.NAME ) as ContentMediator;}
		private function get pWebAPI():WebAPIProxy	{return facade.retrieveProxy( WebAPIProxy.NAME ) as WebAPIProxy;}
		private function get pConfig():ConfigProxy	{return facade.retrieveProxy( ConfigProxy.NAME ) as ConfigProxy;}
//		private function get pGlobalVar():GlobalVarProxy{return facade.retrieveProxy( GlobalVarProxy.NAME ) as GlobalVarProxy;}
	}
}